package com.heima.user.service.impl;

import com.heima.apis.article.IArticleClient;
import com.heima.model.article.dtos.ArticleCollectionDto;
import com.heima.model.user.dtos.LikesDto;
import com.heima.model.user.dtos.ReadDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.service.UserBehaviorService;
import com.heima.utils.thread.ApThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @author ybw
 * @version 1.0
 * @date 2021/12/22 15:36
 */
@Service
@Slf4j
public class UserBehaviorServiceImpl implements UserBehaviorService {
  @Autowired private RedisTemplate<String, String> redisTemplate;

  /**
   * 用户行为点赞
   *
   * @param dto
   * @return
   */
  @Override
  public ResponseResult likeBehavior(LikesDto dto) {
    // 参数校验
    if (ObjectUtils.isEmpty(dto)) {
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    // 获取登录用户
    ApUser loginUser = ApThreadLocalUtil.getUser();
    // 获取点赞状态
    Short operation = dto.getOperation();
    if (operation == 1) {
      // 取消点赞，删除redis中对应的点赞信息
      redisTemplate
          .opsForHash()
          .delete(
              "USER_BEHAVIOR:LIKE:" + loginUser.getId() + ":" + dto.getArticleId(),
              dto.getType().toString());
    } else {
      // 存储到redis中
      redisTemplate
          .opsForHash()
          .put(
              "USER_BEHAVIOR:LIKE:" + loginUser.getId() + ":" + dto.getArticleId(),
              dto.getType().toString(),
              dto.getOperation().toString());
    }
    return ResponseResult.okResult(200, "操作成功");
  }

  /**
   * 用户阅读行为
   *
   * @param dto
   * @return
   */
  @Override
  public ResponseResult readBehavior(ReadDto dto) {
    // 参数校验
    if (ObjectUtils.isEmpty(dto)) {
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    // 查询redis中有无阅读量
    String count =
        (String)
            redisTemplate.opsForHash().get("USER_BEHAVIOR:READ", dto.getArticleId().toString());
    if (count != null && Integer.parseInt(count) > 0) {
      // 之前有，直接加上
      int tmp = Integer.parseInt(count) + dto.getCount();
      count = String.valueOf(tmp);
    } else {
      count = dto.getCount().toString();
    }
    // 存储到redis中
    redisTemplate.opsForHash().put("USER_BEHAVIOR:READ", dto.getArticleId().toString(), count);

    return ResponseResult.okResult(null);
  }

  /**
   * 用户不喜欢
   *
   * @param dto
   * @return
   */
  @Override
  public ResponseResult unlikeBehavior(LikesDto dto) {
    // 参数校验
    if (ObjectUtils.isEmpty(dto)) {
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    // 获取登录用户
    ApUser loginUser = ApThreadLocalUtil.getUser();
    // 获取点赞状态
    Short type = dto.getType();
    if (type == 1) {
      // 取消不喜欢，删除redis中对应的点赞信息
      redisTemplate
          .opsForHash()
          .delete("USER_BEHAVIOR:UNLIKE:" + loginUser.getId(), dto.getArticleId().toString());
    } else {
      // 存储到redis中
      redisTemplate
          .opsForHash()
          .put(
              "USER_BEHAVIOR:UNLIKE:" + loginUser.getId(),
              dto.getArticleId().toString(),
              dto.getType().toString());
      // 删除对应的点赞状态
      redisTemplate
          .opsForHash()
          .delete(
              "USER_BEHAVIOR:LIKE:" + loginUser.getId() + ":" + dto.getArticleId(),
              dto.getType().toString());
    }
    return ResponseResult.okResult(200, "操作成功");
  }
}
